package com.banzhi.shop.model.Impl;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.entity.shoppingcart.Cart;
import com.banzhi.shop.entity.shoppingcart.ChangeNumber;
import com.banzhi.shop.entity.shoppingcart.ChangeStatu;
import com.banzhi.shop.entity.shoppingcart.DeleteGoods;
import com.banzhi.shop.entity.shoppingcart.Order;
import com.banzhi.shop.model.CallBack.shoppingCartCallBack;
import com.banzhi.shop.model.IshoppingCartModel;
import com.banzhi.shop.state.URLString;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by Administrator on 2018/11/10.
 */

public class ChoiceShoppingCartImpl implements IshoppingCartModel {
    @Override
    public void getData(int page, final shoppingCartCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.ChoiceShoppingCartUrl);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        params.addBodyParameter("page", page + "");
        LogUtils.d("shoppingCartImpl", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        Cart cart = gson.fromJson(result, Cart.class);
                        callBack.ongetDataSuccess(cart.getRes());
                    } else {
                        callBack.ongetDataError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("shoppingCartImpl", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(1222)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }

    /**
     * 改变购物车商品的数量
     */
    @Override
    public void changeNumber(String cart_id, int total, final shoppingCartCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.ChoiceUpdateShoppingCartUrl);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        params.addBodyParameter("cart_id", cart_id);
        params.addBodyParameter("total", total + "");
//        LogUtils.d("changeNumber", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        ChangeNumber number = gson.fromJson(result, ChangeNumber.class);
                        callBack.changeNumberSuccess(number.getRes());
                    } else {
                        callBack.changeNumverError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("changeNumber", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(13a)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }

    /**
     * @param cart_id
     * @param callBack 改变商品选中状态的接口
     */
    @Override
    public void changeStatu(String cart_id, String selected, final shoppingCartCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.ChoiceShoppingCartSelectUrl);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        params.addBodyParameter("cart_id", cart_id);
        params.addBodyParameter("selected", selected);
//        LogUtils.d("changeStatu", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        ChangeStatu statu = gson.fromJson(result, ChangeStatu.class);
                        callBack.changeStatuSuccess(statu.getRes());
                    } else {
                        callBack.changeStatuError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("changeStatu", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(1432)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }

    /**
     * @param selected
     * @param callBack 改变所有商品的选中状态
     */
    @Override
    public void changeAllStatu(final String selected, final shoppingCartCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.ChoiceShoppingCartSelectUrl);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        params.addBodyParameter("selected", selected);
        LogUtils.d("changeStatu", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        ChangeStatu statu = gson.fromJson(result, ChangeStatu.class);
                        callBack.changeAllStatuSuccess(statu.getRes());
                    } else {
                        callBack.changeAllStatuError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("changeStatu", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }

    /**
     * @param cart_id
     * @param callBack 删除商品
     */
    @Override
    public void deleteGoods(String cart_id, final shoppingCartCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.ChoiceShoppingCartDeleteUrl);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        params.addBodyParameter("cart_id", cart_id);
//        LogUtils.d("deleteGoods", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        DeleteGoods delete = gson.fromJson(result, DeleteGoods.class);
                        callBack.deleteGoodsSuccess(delete.getRes());
                    } else {
                        callBack.deleteGoodsError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("deleteGoods", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(15)");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }

    /**
     * @param callBack 确认订单
     */
    @Override
    public void QueRenOrder(final shoppingCartCallBack callBack) {
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.ChoiceOrderPreCreateUrl);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        LogUtils.d("QueRenOrder", params.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonobject = new JSONObject(result);
                    if (jsonobject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        Order order = gson.fromJson(result, Order.class);
                        callBack.QueRenOrderSuccess(order.getRes());
                    } else {
                        callBack.QueRenOrderError(jsonobject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("QueRenOrder", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }
}
